﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using log4net;
using McsUtility;

namespace MinerManager.Forms
{
	public partial class MinerLog : DevExpress.XtraEditors.XtraForm
	{
		ILog logger = LogManager.GetLogger(typeof(Program));
		private Common.AgentComponent Agent = null;

		private int MinerId { get; set; }
		private string MinerName { get; set; }

		private int TotalCount = 0;//数据总量
		private int PageIndex = 1;//页码
		private int PageSize = 20;//显示数量
		private int PageCount = 1;//总页码

		public MinerLog(Common.AgentComponent agent, int minerId, string minerName)
		{
			InitializeComponent();
			this.Agent = agent;
			this.MinerId = minerId;
			this.MinerName = minerName;
		}

		#region 窗体事件

		private void MinerLog_Load(object sender, EventArgs e)
		{
			this.Text = "矿机“" + this.MinerName + "”的日志";
			this.Agent.OnNodeServerMinerManagerGetLogs += new EventHandler<ByteEventArgs>(Agent_OnNodeServerMinerManagerGetLogs);

			RequestLogs(1, this.PageSize);
		}

		private void MinerLog_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.Agent.OnNodeServerMinerManagerGetLogs -= new EventHandler<ByteEventArgs>(Agent_OnNodeServerMinerManagerGetLogs);
		}

		#endregion

		#region void Agent_OnNodeServerMinerManagerGetLogs(object sender, ByteEventArgs e)

		private void Agent_OnNodeServerMinerManagerGetLogs(object sender, ByteEventArgs e)
		{
			McsUtility.Models.MinerLogsResponseModel result = ProtocolHelper.ReadPackageBody<McsUtility.Models.MinerLogsResponseModel>(e.Bytes);
			this.TotalCount = result.TotalRows;
			this.PageCount = result.TotalRows % this.PageSize > 0 ? result.TotalRows / this.PageSize + 1 : result.TotalRows / this.PageSize;

			LoadLogs(result.Logs);
		}

		#endregion

		#region TableList按钮事件

		private void BtnFirstPage_Click(object sender, EventArgs e)
		{
			if (this.PageIndex > 1)
			{
				this.PageIndex = 1;
				
				RequestLogs(this.PageIndex, this.PageSize);
			}
		}

		private void BtnBeforPage_Click(object sender, EventArgs e)
		{
			if (this.PageIndex > 1)
			{
				this.PageIndex -= 1;
				this.PageIndex = this.PageIndex >= 1 ? this.PageIndex : 1;
				
				RequestLogs(this.PageIndex, this.PageSize);
			}
		}

		private void BtnAfterPage_Click(object sender, EventArgs e)
		{
			if (this.PageIndex < this.PageCount)
			{
				this.PageIndex += 1;
				this.PageIndex = this.PageIndex <= this.PageCount ? this.PageIndex : this.PageCount;
				
				RequestLogs(this.PageIndex, this.PageSize);
			}
		}

		private void BtnLastPage_Click(object sender, EventArgs e)
		{
			if (this.PageIndex < this.PageCount)
			{
				this.PageIndex = this.PageCount;
				
				RequestLogs(this.PageIndex, this.PageSize);
			}
		}

		#endregion

		#region void TableList_MouseDoubleClick(object sender, MouseEventArgs e)

		private void TableList_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			int[] selectedRows = TableListGridView.GetSelectedRows();
			int selectedIndex = selectedRows.Length > 0 ? selectedRows[0] : -1;
			if (selectedIndex >= 0)
			{
				List<LogModel> infosLog = TableList.DataSource as List<LogModel>;
				if (infosLog.Count > 0)
				{
					MessageBox.Show(infosLog[selectedIndex].Content, "日志");
				}
			}
		}

		#endregion



		#region void RequestLogs(int pageIndex, int pageSize)

		private void RequestLogs(int pageIndex, int pageSize)
		{
			//发送数据
			if (this.Agent.IsStarted)
			{
				McsUtility.Models.MinerLogsRequestModel info = new McsUtility.Models.MinerLogsRequestModel
				{
					MinerID = this.MinerId,
					PageIndex = pageIndex,
					PageRows = pageSize
				};
				byte[] bytes = ProtocolHelper.WritePackage<McsUtility.Models.MinerLogsRequestModel>(ProtoType.MinerManagerGetLogs, info);
				this.Agent.AgentSend(bytes);
			}
			else
			{
				MessageBox.Show("与服务器的链接断开，请重新登录系统。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
			}
		}

		#endregion

		#region void LoadLogs(List<McsUtility.Models.MinerLogsModel> infos)

		private delegate void DelegateLoadLogs(List<McsUtility.Models.MinerLogsModel> infos);

		private void LoadLogs(List<McsUtility.Models.MinerLogsModel> infos)
		{
			if (this.InvokeRequired)
			{
				DelegateLoadLogs d = new DelegateLoadLogs(LoadLogs);
				this.BeginInvoke(d, infos);
			}
			else
			{
				List<LogModel> infosLog = new List<LogModel>();
				for (int i = 0; i < infos.Count; i++)
				{
					infosLog.Add(new LogModel
					{
						Number = (this.PageCount * (this.PageIndex - 1) + 1 + i).ToString(),
						Content = infos[i].Logs,
						CreatedTime = infos[i].CreateTime.ToString("yyyy-MM-dd HH:mm:ss")
					});
				}
				TableList.DataSource = infosLog;

				PageMessage.Text = string.Format("共有{0}条记录 第{1}页/共{2}页", this.TotalCount, this.PageIndex, this.PageCount);
			}
		}

		#endregion

		#region class LogModel 日志信息实体。

		/// <summary>
		/// 日志信息实体。
		/// </summary>
		private class LogModel
		{
			/// <summary>
			/// 序号
			/// </summary>
			public string Number { get; set; }

			/// <summary>
			/// 日志内容
			/// </summary>
			public string Content { get; set; }

			/// <summary>
			/// 创建时间
			/// </summary>
			public string CreatedTime { get; set; }
		}

		#endregion
	}
}